package com.fanshilz.flower2025.framework.mybatisflex;


import cn.hutool.core.util.ObjUtil;
import cn.hutool.core.util.StrUtil;
import com.fanshilz.flower2025.apps.admin.satoken.AdminCurrentUser;
import com.fanshilz.flower2025.apps.admin.satoken.StpAdminUtil;
import com.fanshilz.flower2025.apps.wechatminiapp.satoken.StpWechatUtil;
import com.fanshilz.flower2025.apps.wechatminiapp.satoken.WechatCurrentUser;
import com.fanshilz.flower2025.framework.aop.WebLogAspect;
import com.mybatisflex.annotation.InsertListener;
import com.mybatisflex.annotation.UpdateListener;

public class InsertOrUpdateListener implements UpdateListener, InsertListener {

    @Override
    public void onInsert(Object entity) {
        if (entity instanceof BaseEntity be) {
            if (ObjUtil.isNull(be.getCreateUserId())) {
                String URI = WebLogAspect.requestURIThread.get();
                if (StrUtil.isNotBlank(URI)) {
                    if (URI.startsWith("/admin") && StpAdminUtil.isLogin()) {
                        AdminCurrentUser currentUser = StpAdminUtil.currentUser();
                        be.setCreateUserId(currentUser.getUserId());
                        be.setCreateUserName(currentUser.getNickName());
                    } else if (URI.startsWith("/wechat") && StpWechatUtil.isLogin()) {
                        WechatCurrentUser currentUser = StpWechatUtil.currentUser();
                        be.setCreateUserId(currentUser.getUserId());
                        be.setCreateUserName(currentUser.getUserNickName());
                    }
//                    else if (URI.startsWith("/clewAndroid") && StpClewAndroidUtil.isLogin()) {
//                        ClewAndroidCurrentUser currentUser = StpClewAndroidUtil.currentUser();
//                        be.setCreateUserId(currentUser.getUserId());
//                        be.setCreateUserName(currentUser.getUserName());
//                    }
                }
            }
        }
    }

    @Override
    public void onUpdate(Object entity) {
        if (entity instanceof BaseEntity be) {
            if (ObjUtil.isNull(be.getUpdateUserId())) {
                String URI = WebLogAspect.requestURIThread.get();
                if (StrUtil.isNotBlank(URI)) {
                    if (URI.startsWith("/admin") && StpAdminUtil.isLogin()) {
                        AdminCurrentUser currentUser = StpAdminUtil.currentUser();
                        be.setUpdateUserId(currentUser.getUserId());
                        be.setUpdateUserName(currentUser.getNickName());
                    } else if (URI.startsWith("/wechat") && StpWechatUtil.isLogin()) {
                        WechatCurrentUser currentUser = StpWechatUtil.currentUser();
                        be.setUpdateUserId(currentUser.getUserId());
                        be.setUpdateUserName(currentUser.getUserNickName());
                    }
//                    else if (URI.startsWith("/clewAndroid") && StpClewAndroidUtil.isLogin()) {
//                        ClewAndroidCurrentUser currentUser = StpClewAndroidUtil.currentUser();
//                        be.setUpdateUserId(currentUser.getUserId());
//                        be.setUpdateUserName(currentUser.getUserName());
//                    }
                }
            }
        }
    }

}
